const BASE_STR = 'abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_+';
const BASE_STR_LEN = BASE_STR.length;

/**
 * 字符串工具类
 * @author Zhang
 * @date 2019年1月31日15:35:42
 * @abstract
 */
export abstract class StringUtils {

  /**
   * 生成随机字符串
   * @param length 字符串长度，默认20
   */
  static randomStr(length: number = 20): string {
    let s = '';
    for (let i = 0; i < length; i++) {
      const index = Math.floor(Math.random() * BASE_STR_LEN);
      s += BASE_STR.charAt(index);
    }
    return s;
  }

  /**
   * 生成32位UUID，没有横杠
   * @dependsOn package `node-uuid`
   */
  static uuid(): string {
    return require('node-uuid').v1().replace(/-/g, '');
  }

  /**
   * 首字母转小写
   * @param {string} str
   * @return {string}
   */
  static firstLetter2Lower(str) {
    return str.substr(0, 1).toLowerCase() + str.substr(1);
  }

  /**
   * 判断字符串是否为空或者空白字符串
   * @param str
   */
  static isBlank(str): boolean {
    return str === null || str === undefined || str === '' || str.toString().trim().length === 0;
  }

  static isNotBlank(str: string): boolean {
    return !this.isBlank(str);
  }
}
